/*
    作者：zx
    时间：2023.1.13
    描述：应用程序通过此文件函数调用 调用门 
*/

#include "applib/lib_syscall.h"

void msleep(int ms)
{
    if(ms <= 0)
    {
        return;
    } 
    syscall_args_t args;
    args.id = SYS_SLEEP_ID;
    args.arg0 = ms; 

    sys_call(&args);
}

int get_present_id(void)
{
    syscall_args_t args;
    args.id = SYS_GET_PRESENT_ID;

    return sys_call(&args);
}

void print_msg(char * fmt, int arg)
{
    syscall_args_t args;
    args.id = SYS_PRINT_MSG_ID;
    args.arg0 = (uint32_t)fmt;
    args.arg1 = arg;
    sys_call(&args);
}

int fork(void)
{
    syscall_args_t args;
    args.id = SYS_FORK_ID;
    return sys_call(&args);
}

int execve(const char * name, char * const * argv, char * const *  env)
{
    syscall_args_t args;
    args.arg0 = (int)name;
    args.arg1 = (int)argv;
    args.arg2 = (int)env;
    args.id = SYS_EXECVE_ID;
    return sys_call(&args);
}

int yield(void)
{
    syscall_args_t args;
    args.id = SYS_YIELD_ID;
    return sys_call(&args);
}

int open(const char * name, int flag, ...)
{
    syscall_args_t args;
    args.arg0 = (int)name;
    args.arg1 = (int)flag;
    args.id = SYS_OPEN_ID;
    return sys_call(&args);
}

int read(int file, char * ptr, int len)
{
    syscall_args_t args;  
    args.arg0 = (int)file;
    args.arg1 = (int)ptr;
    args.arg2 = (int)len;
    args.id = SYS_READ_ID;
    return sys_call(&args);
}

int write(int file, char * ptr, int len)
{
    syscall_args_t args;
    args.arg0 = (int)file;
    args.arg1 = (int)ptr;
    args.arg2 = (int)len;
    args.id = SYS_WRITE_ID;
    return sys_call(&args);
}

int close(int file)
{
    syscall_args_t args;
    args.arg0 = (int)file;
    args.id = SYS_CLOSE_ID;
    return sys_call(&args);
}

int lseek(int file, int ptr, int dir)
{
    syscall_args_t args;
    args.arg0 = (int)file;
    args.arg1 = (int)ptr;
    args.arg2 = (int)dir;
    args.id = SYS_LSEEK_ID;
    return sys_call(&args);
}

int isatty(int file)
{
    syscall_args_t args;
    args.id = SYS_ISATTY_ID;
    args.arg0 = (int)file;
    return sys_call(&args);
}

int fstat(int file, struct stat *st)
{
    syscall_args_t args;
    args.id = SYS_FSTAT_ID;
    args.arg0 = (int)file;
    args.arg1 = (int)st;
    return sys_call(&args);
}

//动态分配进程的堆空间      
void * sbrk(ptrdiff_t incr)
{
    syscall_args_t args;
    args.id = SYS_SBRK_ID;
    args.arg0 = (int)incr;
    return (void *)sys_call(&args); 
}

int dup(int file)
{
    syscall_args_t args;
    args.id = SYS_DUP_ID;
    args.arg0 = (int)file;
    return sys_call(&args); 
}

void _exit(int status)
{
    syscall_args_t args;
    args.id = SYS_EXIT_ID;
    args.arg0 = status;
    sys_call(&args); 
}

int wait(int * status)
{
    syscall_args_t args;
    args.id = SYS_WAIT_ID;
    args.arg0 = (int)status;
    return sys_call(&args); 
}
